package virtualstock.game;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import virtualstock.request.GameController;
import virtualstock.util.GameDetails;
import virtualstock.util.PlayerDetails;

/**
 * Servlet implementation class JoinGameServlet
 */
public class JoinGameServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	@EJB
	private GameController gameController;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public JoinGameServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	PrintWriter out = response.getWriter();
    	
    	String strGameID = request.getParameter("gameID");
    	Long gameID = new Long(strGameID);
    	HttpSession session = request.getSession();
    	PlayerDetails currentPlayer = (PlayerDetails)session.getAttribute("playerDetails");
    	Long playerID = currentPlayer.getPlayerID();
    	
    	List<GameDetails> gameList = (List<GameDetails>)session.getAttribute("gameList");
    	List<GameDetails> allAvailableGames = (List<GameDetails>)session.getAttribute("availableGames");
    	//check if the input gameID in the available game list
    	boolean flag1 = false;
    	for(GameDetails gd : gameList){
    		if(gd.getGameID().equals(gameID)){
    			flag1 = true;
    			break;
    		}
    	}
    	
    	if(flag1 == true){
    		//you already join in this game
    		response.sendRedirect("error\\errorAlreadyInGame.html");
    	}else{
    		//check if the game is in the available game list
    		boolean flag2 = false;
    		for(GameDetails gd : allAvailableGames){
        		if(gd.getGameID().equals(gameID)){
        			flag2 = true;
        			break;
        		}
        	}
    		
    		if(flag2 == true){
    			//the game input is in the available game list
    			try{
    	    		System.out.println("playerID:"+playerID+"|gameID"+gameID);
    	    		gameController.addPlayerToGame(playerID, gameID);
    	    		//update the recent game list
    	    		gameList = gameController.getGamesOfPlayer(playerID);
    	    		//for testing
    	    		System.out.println("!!!!!JoinGameServlet:");
    	    	
    	    		for(GameDetails gd : gameList){
    	    			System.out.println(gd.getGameID());
    	    		}
    	    		
    	    		
    	    		allAvailableGames = gameController.getAllAvailableGames();
    				allAvailableGames.removeAll(gameList);
    				
    				session.setAttribute("gameList", gameList);
    				session.setAttribute("availableGames", allAvailableGames);
    				//set request
    				request.setAttribute("playerDetails", currentPlayer);
    				request.setAttribute("gameList", gameList);
    				request.setAttribute("availableGames", allAvailableGames);
    				request.getRequestDispatcher("playerHomePage.jsp").forward(request, response);
    	    	}catch(Exception ex){
    	    		ex.printStackTrace();
    	    	}
    			
    		}else{
    			   	response.sendRedirect("error\\errorGameNotAvailable.html");		
    		}
    	}
    	
    	
    
    }
    
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request, response);
	}

}
